package cn.kgc.controller;

import cn.kgc.entity.User;
import cn.kgc.service.UserService;
import cn.kgc.util.Common;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Rock
 * @create 2020-03-04 21:04
 */
@Controller
@RequestMapping("/sys/user")
public class UserController {
    @Autowired
    private UserService service;

    @RequestMapping(value="/toList",method = RequestMethod.GET)
    public String toList(Model model,User user, Integer pageNum, Integer pageSize){
        if (null==pageNum){
            pageNum=1;
        }
        if (null==pageSize){
            pageSize=5;
        }
        //查询操作
        PageInfo<User> pageInfo = service.queryByNameAndRole(user, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("user",user);
        return "user_list";
    }

    @RequestMapping("/toSave")
    public String toSave(){
        return "user_save";
    }

    @ResponseBody
    @RequestMapping(value = "/queryByUserCode",method = RequestMethod.GET)
    public String queryByUserCode(String userCode){
        List<User> list = service.queryByUserCode(userCode);
        System.out.println(list);
        if (null !=list&&list.size()>0){
            return "error";
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(HttpSession session,User user){
        //获取登录用户对象
        Object object = session.getAttribute(Common.LOGIN_USER);
        if (null == object){
            //登录超时
            return "error2";
        }
        User loginUser = (User)object;
        user.setCreatedBy(loginUser.getId());
        System.out.println(user);
        Integer count = service.save(user);
        if (count>0){
            return "success";
        }
        return "error";
    }
    //登录超时时返回登录页面
    @RequestMapping(value = "/toLogin",method = RequestMethod.GET)
    public String toLogin(){
        return "login";
    }

    @ResponseBody
    @RequestMapping(value = "/queryById",method = RequestMethod.GET,
            produces = "application/json;charset=utf-8")
    public String queryById(Integer id){
        User user = service.queryById(id);
        return JSON.toJSONString(user);
    }
    //跳转到修改密码页面
    @RequestMapping("/toUpdatePassword")
    public String toUpdatePassword(){
        return "update_password";
    }

    //旧密码验证
    @ResponseBody
    @RequestMapping(value = "/checkPassword",method = RequestMethod.GET)
    public String checkPassword(String oldPassword,HttpSession session){
        Object object = session.getAttribute(Common.LOGIN_USER);
        if (null==object){
            return "error1";
        }
        User loginUser = (User)object;
        if (oldPassword.equals(loginUser.getUserPassword())){
            return "success";
        }
        return "error";
    }

    @ResponseBody
    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    public String updatePassword(String userPassword,HttpSession session){
        //获取session中的登录用户
        User loginUser = (User)session.getAttribute(Common.LOGIN_USER);
        User user = new User();
        user.setUserPassword(userPassword);
        user.setId(loginUser.getId());
        Integer count = service.updatePassword(user);
        if (count>0){
            //清除session中的信息
            session.removeAttribute(Common.LOGIN_USER);
            return "success";
        }
        return "error";
    }

    //到修改页面
    @RequestMapping(value = "/toUpdate",method = RequestMethod.GET)
    public String toUpdate(Integer id,Model model){
        User user = service.queryById(id);
        model.addAttribute("user",user);
        return "user_update";
    }

    //处理修改请求]
    @ResponseBody
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update(User user,HttpSession session){
        //从session中获取登录用户
        Object object = session.getAttribute(Common.LOGIN_USER);
        if (object == null) {
            return "error1";
        }
        User loginUser=(User)object;
        user.setModifyBy(loginUser.getId());
        Integer count = service.update(user);
        if (count > 0) {
            return "success";
        }
        return "error";
    }

    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(Integer userId){
        Integer count = service.delete(userId);
        if (count > 0) {
            return "success";
        }
        return "error";
    }

    //退出功能
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout(HttpSession session){
        session.removeAttribute(Common.LOGIN_USER);
        return "login";
    }

    @ResponseBody
    @RequestMapping(value = "/updateStatus",method = RequestMethod.GET)
    public String updateStatus(Integer id){
        Integer count = service.updateStatus(id);
        if (count > 0) {
            return "success";
        }
        return "error";
    }
}
